﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public partial class TZA02  
    {    
        #region ==ZABNR===========
        public static readonly DependencyProperty ZABNRProperty = DependencyProperty.Register("ZABNR",
            typeof(string), typeof(TZA02),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnZABNRChanged));
        public string ZABNR
        {
            get
            {
                return (string)GetValue(ZABNRProperty);
            }
            set
            {
                SetValue(ZABNRProperty, value);
                ORDERNUM = value;
            }
        }
        private static void OnZABNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TZA02;
            if (src != null)
            {
                src.ZABNR = (string)e.NewValue;
            }
        }
        #endregion
        
         
        public TZA02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VZABED();
            this.DataContext = MV;
             InitializeComponent();
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData["ZABMANDANT"] = app.Mandant;
            dwData["ZABBEZ"] = ((VZABED)MV).DES;
            dwData["ZABSPRACHKZ"] = "V ";
            if (LANG=="EN")
            {
                dwData["ZABSPRACHKZ"] = "E ";
            } 
            SResult = ((VZABED)MV).QueryZABED(dwData,2);
            MergeData(SResult, false);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                LFD = "";
                return;
            }
            dwData = TBMain.NewRow();
            ((VZABED)MV).SetUpNewRow(ref dwData, null);
           // LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "ZABED");
            dwData["ZABNR"] = LFD;
            Insert(dwData);
            IsNew = false;
            LFD = "";
            base.NewCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VZABED)MV).DeleteZABED(dwMaster, 1);
            filter = "ZABNR='" + dwMaster["ZABNR"].ToString() + "'";
            vwMaster.View.MoveCurrentTo(-1);
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }
        public override void Copy()
        {
            if (!IsCopy)
            {
                IsCopy = true;
                LFD = "";
                return;
            }
            if (dwMaster == null)
                return;
            DataRow dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            //LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "ZABED");
            dwData["ZABNR"] = LFD;
            Insert(dwData);
            IsCopy = false;
            LFD = "";
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (LANG.Trim().Length == 0)
            {
                MessageBox.Show("请先选择语言");
                return;
            }
            base.EditCommand(sender, e);
        }
           
        public   void   ChgKeyCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                if (LFD == "000")
                    return;
                if (LFD == "")
                    return;
                filter = "ZABNR='" + LFD + "'";
                rows = TBMain.Select(filter);
                if (rows.Length > 0)
                {
                    throw new Exception("编号已经存在");
                }
                dwData = TBMain.NewRow();
                dwData.ItemArray = dwMaster.ItemArray;
                dwData["ZABBEZ"] = LFD;
                ((VZABED)MV).UZA03(dwData);
                dwMaster["ZABNR"] = LFD;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }

            }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            Copy();
            base.CopyCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        { 
            base.SaveCommand(sender, e);
            IsSaved = false;
        } 
        public override void LOADBYAUF()
        {  
            if (!IsSaved)
                return;
            if (ORDERNUM.Length < 3)
                return;
            if (ORDERNUM != KEY)
                return;
          
            dwData = TBMain.NewRow();
            dwData["ZABMANDANT"] = app.Mandant;
            dwData["ZABBEZ"] = DES;
            dwData["ZABSPRACHKZ"] = LANG.PadRight(2);
             dwData["ZABNR"] = " AND ZABNR='"+ORDERNUM+"'";
            SResult = ((VZABED)MV).QueryZABED(dwData, 2);         
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
        }
    }
}
